package com.xdf.zipperdye.service;

import com.xdf.zipperdye.enums.BillType;
import com.xdf.zipperdye.model.BillOrder;
import com.xdf.zipperdye.model.OrderDetail;
import com.xdf.zipperdye.model.OrderMain;
import com.xdf.zipperdye.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class BillOrderServiceImpl implements BillOrderService {
    @Autowired
    private OrderMainService mainService;
    @Autowired
    private OrderDetailService detailService;
    @Autowired
    private BillCodeService billCodeService;
    @Autowired
    private BillOrderChecker checker;

    @Transactional
    @Override
    public BillOrder insert(BillOrder record) {
        if(BillOrderChecker.check4Save(record) == false) return null;
        OrderMain main = record.getMain();
        String billCode = billCodeService.getNewBillCode(BillType.CLIENT_ORDER.val());
        main.setBill_code(billCode);
        mainService.insertSelective(main);
        Integer id = main.getIid();
        List<OrderDetail> details = record.getDetails();
        details.forEach(item -> {
            item.setParent_id(id);
        });

        detailService.multiInsert(details);

        // 构建新增的单据并返回
        main = mainService.selectByPrimaryKey(main.getIid());
        details = detailService.selectByPid(main.getIid());

        return new BillOrder(main,details);
    }

    @Transactional
    @Override
    public int deleteByPrimaryKey(Integer iid) {
        if(checker.check4DelMain(iid)==false) throw new BusinessException("有关联数据，不可删除！");
        int ret1 = detailService.deleteByPid(iid);
        int ret2 = mainService.deleteByPrimaryKey(iid);
        return 1;
    }

    @Transactional
    @Override
    public BillOrder update(BillOrder record) {
        if(BillOrderChecker.check4Update(record)==false) return null;
//        detailService.multiUpdate(record.getDetails());

        Integer iid = record.getMain().getIid();
        // 有则修改，无则新增
        List<OrderDetail> details = record.getDetails();
        details.forEach(item -> item.setParent_id(iid));
        detailService.multiSave(details);
        mainService.updateByPrimaryKeySelective(record.getMain());


        OrderMain main = mainService.selectByPrimaryKey(iid);
        details = detailService.selectByPid(iid);
        BillOrder ret = new BillOrder();
        ret.setMain(main);
        ret.setDetails(details);
        return ret;
    }

    @Override
    public BillOrder selectByPrimaryKey(Integer iid) {
        OrderMain main = mainService.selectByPrimaryKey(iid);
        List<OrderDetail> details = detailService.selectByPid(iid);
        BillOrder ret = new BillOrder();
        ret.setMain(main);
        ret.setDetails(details);
        return ret;
    }

    @Override
    public BillOrder selectByBillCode(String billCode) {
        List<OrderMain> mains = mainService.selectByBillCode(billCode);
        if(mains.size()<=0) return null;
        OrderMain main = mains.get(0);
        Integer iid = main.getIid();
        List<OrderDetail> details = detailService.selectByPid(iid);
        BillOrder ret = new BillOrder();
        ret.setMain(main);
        ret.setDetails(details);
        return ret;
    }


}
